<!-- 填报hub -->
<template>
  <div class="innerPageTable-box-OneTwo value-set-list-wrap innerPageTable-out-flex">
    <SelfAdaption>
      <Form
        :model="searchForm"
        ref="serviceForm"
        slot="adaption-bar"
        label-position="top"
        class="ivu-form-no-margin-bottom"
        inline
      >
        <Form-item prop="label" :label="'报表名称'">
          <Input type="text" v-model.trim="searchForm.label" :placeholder="'请输入'" @on-enter="searchValueSet"></Input>
        </Form-item>

        <!-- 查询\重置按钮 -->
        <Form-item prop="button" class="search-solt-btn">
          <RPButton optType="search" type="text" @click="searchValueSet()" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="valueSetTable" :config="pageTableConfig"> </InnerPageTable>
  </div>
</template>

<script>
import permission from '@/mixin/permission'
import { tableColumns } from './columns'
import { FORM_TYPE_INDICATOR, FORM_TYPE_TASK, FORM_TYPE_DECLARATION, FORM_TYPE_RATING } from '@/views/formConst'
import declarationFormList from '@/views/menuList/declaration/formList'
import ratingList from '@/views/menuList/rating/index'
import summary from '@/views/menuList/rating/summary'
import formCenterApi from '@/api/formCenter.js'
export default {
  name: 'menuList',
  mixins: [permission],
  props: {
    menus: {
      type: Array,
      default: () => []
    },
    custom: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      //有制表员权限
      isFormCreate: false,
      searchForm: {
        label: ''
      },
      pageTableConfig: {
        loadDataFn: this.getServiceList,
        columns: tableColumns.bind(this)(),
        autoFirst: false //默认 就是 false, 要配置在这里
      }
    }
  },
  mounted() {
    this.init()
    this.searchValueSet()
  },
  methods: {
    init() {
      this.isFormCreate = this.hasPermission('FORM_CREATE')
    },
    opt(row) {
      this.enterFormList(row)
    },
    view(row) {
      this.enterFormList(row, true)
    },
    summary(row) {
      if (row.native) {
        row.onSummary(row)
      } else {
        new this.$sidelipPage(row.summaryView, {
          props: {
            row
          }
        })
      }
    },
    // 查看
    enterFormList(row, onlyView) {
      //获取菜单下内容对应的类型
      if (row.to) {
        window.open(row.to, '_blank')
      } else if (row.native) {
        if (onlyView) {
          row.onView(row)
        } else {
          row.onOpt(row)
        }
      } else {
        new this.$sidelipPage(row.target, {
          props: {
            row,
            onlyView: onlyView ? true : false
          }
        })
      }
    },
    getServiceList(tableObj) {
      let { responseProcess, param } = tableObj
      let records = this.menus.filter(item => item.label.includes(this.searchForm.label))
      formCenterApi
        .parentMenuPermission(this.menus.map(item => item.value))
        .then(res => {
          records.forEach(item => {
            const m = res.data.data.find(data => data.menu == item.value)
            //智数部用户都有查看按钮
            if (m) {
              if (m.type == FORM_TYPE_DECLARATION) {
                //申报类
                item.target = declarationFormList
                item.data = m
                item.opt = m.permission.operate
                item.view = this.isFormCreate
              } else if (m.type == FORM_TYPE_RATING) {
                //评分类,制表人有查看和汇总，评委只有操作按钮
                //制表人在操作中点击晋级
                item.target = ratingList
                item.summaryView = summary
                item.data = m
                item.opt = m.permission.operate || m.permission.formCreator
                item.view = this.isFormCreate
                item.hasSummary = true
                item.summary = this.isFormCreate
              } else if (m.type == FORM_TYPE_INDICATOR) {
                // 指标类
                item.target = declarationFormList
                item.summaryView = summary
                item.data = m
                item.opt = m.permission.operate || m.permission.formCreator
                item.view = this.isFormCreate
                item.hasSummary = true
                item.summary = this.isFormCreate
              } else if (m.type == FORM_TYPE_TASK) {
                //任务类
              }
            } else {
              //没有发布，则按钮置灰
              if (item.to) {
                //低代码做的模块
                item.view = true
              } else if (item.native) {
                if (this.custom && this.custom.find(c => c.label === item.label)) {
                  const v = this.custom.find(c => c.label === item.label)
                  item.onOpt = v.onOpt
                  item.onView = v.onView
                  item.onSummary = v.onSummary
                  item.opt = v.operate
                  item.view = v.view
                  item.summary = v.summary
                  item.hasSummary = v.hasSummary
                } else {
                  console.error('未配置native模块')
                }
              } else {
                item.opt = false
                item.view = false
                item.summary = false
              }
            }
          })
          responseProcess({
            pageNum: 1,
            pageSize: 10,
            pages: 1,
            records,
            total: records.length
          })
        })
        .catch(e => {})
    },
    searchValueSet() {
      this.$refs['valueSetTable'].load(this.searchForm)
    }
  },
  watch: {},
  directives: {}
}
</script>
